﻿using Audit.Core;
using Dapper_Core.BLL.Interface;
using Dapper_Core.Common;
using Dapper_Core.DAL.Impl;
using Dapper_Core.Model;
using Dapper_Core.Table;
using ExpressionToWhere;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Dapper_Core.BLL.Impl
{
    public class Sys_Role_MenuBusiness : BaseRepository<Sys_Role_Menu>, ISys_Role_MenuBusiness
    {
        public Sys_Role_MenuBusiness()
        {

        }

        public Sys_Role_MenuBusiness(TransactionContext transactionContext) : base(transactionContext)
        {

        }

        public bool UpdateRelation(string addMenuIds, string deleteMenuIds, string roleCode, string creator)
        {
            TransactionContext transactionContext = null;
            try
            {
                if (string.IsNullOrEmpty(addMenuIds) && string.IsNullOrEmpty(deleteMenuIds)) return true;
                if (string.IsNullOrEmpty(roleCode)) return true;
                List<string> addMenuList = (addMenuIds ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
                List<string> deleteMenuList = (deleteMenuIds ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
                if (addMenuList.Count < 1 && deleteMenuList.Count < 1) return true;
                List<Sys_Role_Menu> roleMenuList = new List<Sys_Role_Menu>();
                DateTime createDate = DateTime.Now;
                foreach (string menuId in addMenuList)
                {
                    Sys_Role_Menu roleMenu = new Sys_Role_Menu();
                    roleMenu.Id = Guid.NewGuid().ToString();
                    roleMenu.RoleCode = roleCode;
                    roleMenu.MenuId = menuId;
                    roleMenu.Creator = creator;
                    roleMenu.CreateDate = createDate;
                    roleMenuList.Add(roleMenu);
                }

                transactionContext = InitialTransaction();
                //开启事务
                transactionContext.BeginTransaction();
                if (deleteMenuList.Count > 0)
                {
                    Expression<Func<Sys_Role_Menu, bool>> exp = roleMenu => roleMenu.RoleCode == roleCode && roleMenu.MenuId.In(deleteMenuList);
                    base.DeleteList(exp);
                }
                base.AddList(roleMenuList);
                //记录审计日志
                using (AuditScope scope = AuditScope.Create("Sys_Role_Menu:Update", () => new { roleCode = roleCode, addMenuIds = addMenuIds, deleteMenuIds = deleteMenuIds }))
                {
                    scope.Comment("更新“角色 - 系统资源”对应关系");
                    scope.SetCustomField("Module", "Sys_Role_Menu");
                    scope.SetCustomField("Operator", creator);
                    scope.SetCustomField("Operation", Operation.Update);
                }
                //提交事务
                transactionContext.Commit();
                return true;
            }
            catch (Exception ex)
            {
                //回滚事务
                transactionContext.Rollback();
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "更新“角色 - 系统资源”对应关系失败", "addMenuIds=" + addMenuIds + "，deleteMenuIds=" + deleteMenuIds + "，roleCode=" + roleCode, ex, "DapperCore");
                return false;
            }
        }
    }
}
